<?php

namespace User;

use C\L\WebUserController;
use C\M\CouponReceive;

class InfoController extends WebUserController
{

  /**
   * 我的-个人中心
   **/
    public function indexAction()
    {
        $userId = $this->uid;

        $user                = $this->s_user->search($userId);
        $unreadMessagesCount = $this->s_message->countUserUnreadMessages($userId);
        $unuseCouponCount    = CouponReceive::availableCouponsCount($this->uid, PHP_INT_MAX);

        $data = [
        'money'                 => $user['money'] ?? '0.00',
        'credit'                => $user['credit'] ?? null,
        'nick_name'             => $user['nick_name'] ?? null,
        'is_auth'               => $user['is_auth'] ?? 'N',
        'avatar'                => $this->s_config->get('web')['user_logo'],
        'vip_name'              => $this->s_level->getLevel($this->uid)['name'],
        'unread_messages_count' => $unreadMessagesCount,
        'unuse_coupon_count'    => $unuseCouponCount,
        ];

        $this->success($data);
    }

  /**
   * 实名认证
   **/
    public function authAction()
    {
        $name   = $this->getValue('name', true);
        $idcard = $this->getValue('idcard', true);

        if (!$this->s_user->auth($this->uid, $name, $idcard)) {
            return $this->error();
        }

        $this->ssid->set('is_auth', 'Y');
        return $this->success();
    }

    public function infoAction()
    {
        $user = $this->s_user->search($this->uid, [], [
        'is_auth', 'name', 'idcard', 'mobile'
        ]);
        $this->success($user);
    }

  //我的邀请
    public function shareAction()
    {
        $uid = $this->uid;
        $inviteTypes = [
        'register_superior_money',
        'group_buying_confirm_order_superior_reward',
        'group_buying_confirm_user_order_superior_reward',
        ];

        $moneySum = $this->s_funds->getSum(
            'money',
            [
            'uid'   => $uid,
            'stype' => $inviteTypes,
            ],
            [
            'uid'   => '=',
            'stype' => 'in'
            ]
        );

        $path = '/api/api/qrcode?uid=' . $this->uid . '&x=' . substr(md5(rand(1, 9999)), 0, 9);
        $data = [
        'mobile'          => $this->s_user->getValue('mobile', $uid),
        'share_user_num'  => $this->s_user->getCount(['t_uid' => $uid]),
        'share_money'     => $moneySum,
        'share_image_url' => $this->convertAssetsUrl($path),
        'share_user'      => [],
        ];

        $shareUserArray = $this->s_user->searchPage($uid, 't_uid', [], '', 1, 50);
        if (!empty($shareUserArray)) {
            foreach ($shareUserArray as $item) {
                $data['share_user'][] = [
                'mobile' => substr($item['mobile'], 0, 3) . '****' . substr($item['mobile'], -4, 4),
                'date' => date('Y-m-d H:i', $item['addtime'])
                ];
            }
        }

        $this->success($data);
    }

    public function resetPasswordAction()
    {
        $passwd = $this->getValue('password', true, 'string');
        $npasswd = $this->getValue('npassword', true, 'string');

        if ($this->s_user->repasswd($this->uid, $passwd, $npasswd)) {
            $this->success();
        }

        $this->error();
    }

  /**
   * 短信方式修改登录密码
   */
    public function codeResetPwdAction()
    {
        $code = $this->getValue('code', true, 'string');
        $mobile = $this->getValue('mobile', true, 'string');
        $npasswd = $this->getValue('npassword', true, 'string');
        if ($this->s_user->codeRePwd($this->uid, $mobile, $code, $npasswd)) {
            $this->success();
        }
        $this->error();
    }

    public function resetPayPasswordAction()
    {
        $passwd = $this->getValue('password', false, 'string');
        $npasswd = $this->getValue('npassword', true, 'string');

        if ($this->s_user->repaypasswd($this->uid, $passwd, $npasswd)) {
            $this->success();
        }

        $this->error();
    }

  /**
   * 短信验证方式修改支付密码
   */
    public function codeResetPayPwdAction()
    {
        $code = $this->getValue('code', true, 'string');
        $mobile = $this->getValue('mobile', true, 'string');
        $npasswd = $this->getValue('npassword', true, 'string');
        if ($this->s_user->codeRePayPwd($this->uid, $mobile, $code, $npasswd)) {
            $this->success();
        }
        $this->error();
    }
}
